/*
 * @(#) $Header$
 *
 * Copyright (C) 2006  Daniel Léonard
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package ca.forklabs.baselib.swing.event;

import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import ca.forklabs.baselib.swing.event.AbstractSwitchListener;
import ca.forklabs.baselib.swing.event.DocumentAdapter;

/**
 * Class {@code SwitchDocumentListener} holds action listeners with only one
 * considered active. The event is always forwarded to the active listener. The
 * default listener is {@link #DO_NOTHING_DOCUMENT_LISTENER}.
 *
 * @author   <a href="mailto:forklabs at gmail.com?subject=ca.forklabs.baselib.swing.event.SwitchDocumentListener">Daniel Léonard</a>
 * @version $Revision$
 */
public class SwitchDocumentListener extends AbstractSwitchListener<DocumentListener> implements DocumentListener {

//---------------------------
// Class variables
//---------------------------

   /**
    * The do-nothing action listener.
    */
   public static final DocumentListener DO_NOTHING_DOCUMENT_LISTENER = new DocumentAdapter() { /* nothing */ };


//---------------------------
// Constructor
//---------------------------

   /**
    * Constructor.
    */
   public SwitchDocumentListener() {
   // nothing
      }


//---------------------------
// Implemented methods from ca.forklabs.baselib.swing.event.AbstractSwitchListener
//---------------------------

   /**
    * Gets the default listener for the switch.
    * @return   the listener.
    */
   @Override
   protected DocumentListener getDefaultListener() {
      DocumentListener default_listener = SwitchDocumentListener.DO_NOTHING_DOCUMENT_LISTENER;
      return default_listener;
      }


//---------------------------
// Instance methods
//---------------------------

   /**
    * Adds a new action listener to the switch.
    * @param   key    the key of the listener.
    * @param   listener   the listener.
    */
   public void addDocumentListener(Object key, DocumentListener listener) {
      this.addListener(key, listener);
      }

   /**
    * Removes an action listener from the switch.
    * @param   key    the key of the listener.
    */
   public void removeActionListener(Object key) {
      this.removeListener(key);
      }


//---------------------------
// Implemented methods from javax.swing.event.DocumentListener
//---------------------------

   /**
    * Forwards the event to the current listener.
    * @param   de   the event.
    */
   @Override
   public void changedUpdate(DocumentEvent de) {
      DocumentListener listener = this.getActiveListener();
      listener.changedUpdate(de);
      }

   /**
    * Forwards the event to the current listener.
    * @param   de   the event.
    */
   @Override
   public void insertUpdate(DocumentEvent de) {
      DocumentListener listener = this.getActiveListener();
      listener.insertUpdate(de);
      }

   /**
    * Forwards the event to the current listener.
    * @param   de   the event.
    */
   @Override
   public void removeUpdate(DocumentEvent de) {
      DocumentListener listener = this.getActiveListener();
      listener.removeUpdate(de);
      }

   }
